Socket
Socket
Sign inDemoInstall

promise-polyfill

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-polyfill

Lightweight promise polyfill. A+ compliant


Version published
Weekly downloads
3.5M
increased by0.91%
Maintainers
1
Weekly downloads
 
Created

What is promise-polyfill?

The promise-polyfill npm package is a lightweight implementation of the ES6 Promise specification. It is designed to provide a simple and efficient way to use Promises in environments that do not natively support them, such as older browsers.

What are promise-polyfill's main functionalities?

Basic Promise Usage

This code demonstrates the basic usage of a Promise. It creates a new Promise that resolves with the message 'Success!' after 1 second, and then logs the message to the console.

const Promise = require('promise-polyfill');

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('Success!');
  }, 1000);
});

promise.then((message) => {
  console.log(message); // 'Success!'
});

Chaining Promises

This code demonstrates how to chain multiple .then() calls to handle a sequence of asynchronous operations. Each .then() call receives the resolved value from the previous Promise and returns a new value.

const Promise = require('promise-polyfill');

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(1);
  }, 1000);
});

promise
  .then((value) => {
    console.log(value); // 1
    return value + 1;
  })
  .then((value) => {
    console.log(value); // 2
    return value + 1;
  })
  .then((value) => {
    console.log(value); // 3
  });

Handling Errors

This code demonstrates how to handle errors in Promises using the .catch() method. If the Promise is rejected, the error message is logged to the console.

const Promise = require('promise-polyfill');

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject('Error occurred!');
  }, 1000);
});

promise
  .then((value) => {
    console.log(value);
  })
  .catch((error) => {
    console.error(error); // 'Error occurred!'
  });

Other packages similar to promise-polyfill

Keywords

FAQs

Package last updated on 25 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc